package com.cy.service.impl;

import com.cy.entity.Cart;
import com.cy.entity.Product;
import com.cy.mapper.CartMapper;
import com.cy.mapper.ProductMapper;
import com.cy.service.ICartService;
import com.cy.service.IProductService;
import com.cy.service.ex.AccessDeniedException;
import com.cy.service.ex.CartNotFoundException;
import com.cy.service.ex.InsertException;
import com.cy.service.ex.UpdateException;
import com.cy.util.JsonResult;
import com.cy.vo.CartVO;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 时间：2023/1/7
 */
@Service
public class CartServiceImpl implements ICartService {

    @Resource
    private CartMapper cartMapper;

    @Resource
    private IProductService productService;

    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
        //查询当前要添加的这个购物是否在表中已存在
        Cart result = cartMapper.findByUidAndPid(uid,pid);
        Date date = new Date();
        if(result == null){//表示这商品从来没有被添加到购物车中，则进行新增操作
            //创建一个cart对象
            Cart cart = new Cart();
            //补全数据

            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            //价格:来自于商品中的数据
            Product product = productService.findById(pid);
            cart.setPrice(product.getPrice());
            //补全4项日志
            cart.setCreatedUser(username);
            cart.setModifiedUser(username);
            cart.setCreatedTime(date);
            cart.setModifiedTime(date);
            cartMapper.insert(cart);

            //执行输入的插入操作
            Integer rows = cartMapper.insert(cart);
            if(rows != 1){
                throw new InsertException("插入数据时产生未知异常");
            }
        }else{//表示当前的商品在购物车中已经存在，则更新这条数据的num值
            Integer num = result.getNum() + amount;
            Integer rows = cartMapper.updateNumByCid(result.getCid(), num, username,date);
            if(rows != 1){
                throw new UpdateException("更新数据时产生的异常");
            }

        }
    }

    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        return cartMapper.findVOByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if(result == null){
            throw new CartNotFoundException("数据不存在");
        }
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num =result.getNum()+1;
        Integer rows = cartMapper.updateNumByCid(cid,num,username,new Date());
        if(rows != 1){
            throw new UpdateException("更新数据失败");
        }
        return num;

    }

    @Override
    public List<CartVO> getVOByCids(Integer uid, Integer[] cids) {
        List<CartVO> list = cartMapper.findVOByCids(cids);
        Iterator<CartVO> it = list.iterator();
        while(it.hasNext()){
            CartVO cartVO = it.next();
            if(!cartVO.getUid().equals(uid)){//表示当前数据不属于当前用户
                //从集合中移除这个元素
                list.remove(cartVO);
            }
        }
        return list;
    }


}
